package com.lloydfinch.composetour.vm

import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel
import com.lloydfinch.composetour.bean.TodoItem

/**
 * Name: TodoViewModel
 * Author: lloydfinch
 * Function: TodoViewModel
 * Date: 2021/11/22 6:35 下午
 * Modify: lloydfinch 2021/11/22 6:35 下午
 */
class TodoViewModel : ViewModel() {
    //private var _todoItems: MutableLiveData<MutableList<TodoItem>> = MutableLiveData(LinkedList<TodoItem>())
    //val todoItems: LiveData<MutableList<TodoItem>> = _todoItems
    //
    //fun addItem(item: TodoItem) {
    //    _todoItems.value = _todoItems.value!!.toMutableList().also { it.add(item) }
    //}
    //
    //fun removeItem(item: TodoItem) {
    //    _todoItems.value = _todoItems.value!!.toMutableList().also { it.remove(item) }
    //}

    /**
     * 替换为State，专用于Compose的
     */
    var todoItems = mutableStateListOf<TodoItem>()
        private set

    fun addItem(item: TodoItem) {
        todoItems.add(item)
    }

    fun removeItem(item: TodoItem) {
        todoItems.remove(item)
    }
}